// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// A simple main() that segments an image into two labels and displays the
// result

#include <cstdlib>
#include <iomanip>
#include <ios>
#include <sstream>
#include "compat.h"
#include "image_manipulator.h"
#include "image.h"
#include "image_utilities.h"

using namespace harmonizer;

DEFINE_string(input_filename, "", "Name of input image");
DEFINE_string(output_prefix, "", "Prefix of output (segmentation) image");
DEFINE_double(lambda, 1, "Lambda value used for mincut");
DEFINE_string(segments, "", "Comma-separated list of segments (no spaces)");

int main(int argc, char **argv) {
  google::ParseCommandLineFlags(&argc, &argv, true);

  std::stringstream outputFilename;
  outputFilename << FLAGS_output_prefix << std::setiosflags(std::ios::fixed)
                 << std::setprecision(2) << FLAGS_lambda << ".png";

  // Convert FLAGS_segments to a vector of floats
  std::vector<float> seg_boundaries;
  vector<string> seg_boundaries_str = SplitString(FLAGS_segments, ",");
  for (string boundary : seg_boundaries_str) {
    float val = 0;
    val = strtof(boundary.c_str(), NULL);
    seg_boundaries.push_back(val);
  }
  CHECK(seg_boundaries.size() > 0 && (seg_boundaries.size() % 2 == 0));

  Image outputImage;
  ImageManipulator manip1(FLAGS_input_filename);
  HueRange hue_range(seg_boundaries);

  CHECK(manip1.CreateLabelImage(hue_range, &outputImage, FLAGS_lambda));
  CHECK(WriteImage(outputImage, outputFilename.str().c_str()));

  return 0;
}

